﻿package com.tvein.sanguo.swc.action.role
{
	//flash
	import flash.display.MovieClip;
	import flash.display.Loader;
	import flash.net.URLRequest;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.display.StageAlign;
	import flash.display.StageScaleMode;
	//A3D
	//custom
	import com.tvein.sanguo.swc.common.OverLay;
	import com.tvein.sanguo.swc.common.SwcEvent;
	import com.tvein.sanguo.swc.common.MP3Click;
//
	public class RoleDetail extends MovieClip
	{
		public static const NAME:String = "Role-Detail";
		
		private var sourceServer:String;
		private var effectVol:int = 100;
		private var overLay:OverLay;
		
		public function RoleDetail()
		{
			stage?init(null):addEventListener(Event.ADDED_TO_STAGE, init);
		}

		private function init(evt:Event):void
		{
			removeEventListener(Event.ADDED_TO_STAGE , init);
			addEventListener(Event.REMOVED_FROM_STAGE, _onRemoved);
			stage.addEventListener(Event.RESIZE, _resizeUI);
			
			stage.scaleMode = StageScaleMode.NO_SCALE;
			stage.align = StageAlign.TOP_LEFT;
			
			overLay = new OverLay();			
			this.addChild(overLay);
			this.setChildIndex(overLay, 0);
			
			btnArmy.visible = false;
			
			btnClose.addEventListener(MouseEvent.CLICK, onClick);
			btnGoods.addEventListener(MouseEvent.CLICK, onClick);
			btnItem.addEventListener(MouseEvent.CLICK, onClick);
			btnSkill.addEventListener(MouseEvent.CLICK, onClick);
			btnArmy.addEventListener(MouseEvent.CLICK, onClick);
			
			mcFrame.reSize(600, 450);
			
			_resizeUI();
		}

		public function console(Type:String, params:Object):void
		{
			switch (Type)
			{
				case "setSource":
					sourceServer = params.Address;
					break;
				case "setEffect":
					effectVol = params.Volume;
					break;
				case "setData":
					setData(params);
					break;
			}
		}
		
		private function setData(params:Object):void
		{
			if (params.Name == "GetRoleDetail")
			{
				txtName.text = params.Process[0].RoleName;
				txtStyleName.text = params.Process[0].StyleName;
				(params.Process[0].Sex == 1)?txtSex.text = "男":txtSex.text = "女";
				txtStatus.text = params.Process[0].Status;
				txtCamp.text = params.Process[0].CampName;
				txtOfficial.text = params.Process[0].OfficialRank;
				
				if (params.Process[0].StaminaAdd == 0)
				{
					txtStamina.text = params.Process[0].StaminaNum;
				}
				else
				{
					if (params.Process[0].StaminaAdd > 0)
					{
						txtStamina.text = params.Process[0].StaminaNum + "+" + params.Process[0].StaminaAdd;
					}
					else
					{
						txtStamina.text = params.Process[0].StaminaNum + "-" + (-params.Process[0].StaminaAdd);
					}
				}
				
				if (params.Process[0].EnergyAdd == 0)
				{
					txtEnergy.text = params.Process[0].EnergyNum;
				}
				else
				{
					if (params.Process[0].EnergyAdd > 0)
					{
						txtEnergy.text = params.Process[0].EnergyNum + "+" + params.Process[0].EnergyAdd;
					}
					else
					{
						txtEnergy.text = params.Process[0].EnergyNum + "-" + (-params.Process[0].EnergyAdd);
					}
				}
				
				if (params.Process[0].ForceAdd == 0)
				{
					txtForce.text = params.Process[0].ForceNum;
				}
				else
				{
					if (params.Process[0].ForceAdd > 0)
					{
						txtForce.text = params.Process[0].ForceNum + "+" + params.Process[0].ForceAdd;
					}
					else
					{
						txtForce.text = params.Process[0].ForceNum + "-" + (-params.Process[0].ForceAdd);
					}
				}
				
				if (params.Process[0].IntelligenceAdd == 0)
				{
					txtIntelligence.text = params.Process[0].IntelligenceNum;
				}
				else
				{
					if (params.Process[0].IntelligenceAdd > 0)
					{
						txtIntelligence.text = params.Process[0].IntelligenceNum + "+" + params.Process[0].IntelligenceAdd;
					}
					else
					{
						txtIntelligence.text = params.Process[0].IntelligenceNum + "-" + (-params.Process[0].IntelligenceAdd);
					}
				}
				
				if (params.Process[0].CommandAdd == 0)
				{
					txtCommand.text = params.Process[0].CommandNum;
				}
				else
				{
					if (params.Process[0].CommandAdd > 0)
					{
						txtCommand.text = params.Process[0].CommandNum + "+" + params.Process[0].CommandAdd;
					}
					else
					{
						txtCommand.text = params.Process[0].CommandNum + "-" + (-params.Process[0].CommandAdd);
					}
				}
				
				if (params.Process[0].PoliticsAdd == 0)
				{
					txtPolitics.text = params.Process[0].PoliticsNum;
				}
				else
				{
					if (params.Process[0].PoliticsAdd > 0)
					{
						txtPolitics.text = params.Process[0].PoliticsNum + "+" + params.Process[0].PoliticsAdd;
					}
					else
					{
						txtPolitics.text = params.Process[0].PoliticsNum + "-" + (-params.Process[0].PoliticsAdd);
					}
				}				
				
				txtMerit.text = params.Process[0].MeritNum;
				txtRepute.text = params.Process[0].ReputeNum;
				
				var headloader:Loader = new Loader();
				headloader.load(new URLRequest(sourceServer + "/SanGuo/role/head/" + params.Process[0].ImageID + ".png"));
				headloader.contentLoaderInfo.addEventListener(Event.COMPLETE, _onHeadLoad);
			}
		}

		private function _onHeadLoad(evt:Event):void
		{
			evt.target.removeEventListener(Event.COMPLETE, _onHeadLoad);
			
			evt.target.content.x = 38;
			evt.target.content.y = 38;
			
			this.addChild(evt.target.content);
		}

		private function _onRemoved(evt:Event):void
		{
			removeEventListener(Event.REMOVED_FROM_STAGE, _onRemoved);
			stage.removeEventListener(Event.RESIZE, _resizeUI);
			
			btnClose.removeEventListener(MouseEvent.CLICK, onClick);
			btnGoods.removeEventListener(MouseEvent.CLICK, onClick);
			btnItem.removeEventListener(MouseEvent.CLICK, onClick);
			btnSkill.removeEventListener(MouseEvent.CLICK, onClick);
			btnArmy.removeEventListener(MouseEvent.CLICK, onClick);
		}

		private function _resizeUI(evt:Event = null):void
		{
			overLay.x = overLay.y = overLay.width = overLay.height = 0;
			
			this.x = (stage.stageWidth - this.width) * .5
			
			if ((stage.stageHeight - this.height) > 150)
			{
				this.y = (stage.stageHeight - this.height) * .3;
			}
			else
			{
				this.y = 45;
			}
			
			overLay.x = 0 - this.x;
			overLay.y = 0 - this.y;
			overLay.width = stage.stageWidth;
			overLay.height = stage.stageHeight;
		}

		private function onClick(evt:MouseEvent):void
		{
			var mp3Click:MP3Click = new MP3Click();
			mp3Click.playback(effectVol * .01);
			
			switch(evt.target.name)
			{
				case "btnClose":
					dispatchEvent(new SwcEvent(NAME, ["-close", "RoleDetail"]));
					break;
				case "btnGoods":
					dispatchEvent(new SwcEvent(NAME, ["-showBox", ["roleGoods"]]));
					break;
				case "btnItem":
					dispatchEvent(new SwcEvent(NAME, ["-showBox", ["roleItem"]]));
					break;
				case "btnSkill":
					dispatchEvent(new SwcEvent(NAME, ["-showBox", ["roleSkill"]]));
					break;
				case "btnArmy":
					dispatchEvent(new SwcEvent(NAME, ["-showBox", ["roleArmy"]]));
					break;
			}
		}
	}
}